//
//  CarHistoryCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/25/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class CarHistoryCell: UITableViewCell, MainVCCell {

    // MARK: - Outlets

    private lazy var separatorView = CellSeparator()
    
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        lbl.textAlignment = .left
        lbl.sizeToFit()
        lbl.textColor = .black
        lbl.text = "История авто"
        return lbl
    }()
    
    private lazy var itemsStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .fill
        stack.distribution = .fillEqually
        return stack
    }()
    
    private lazy var moreBtn: MoreButton = {
        let btn = MoreButton()
        btn.setUp(text: "Смотреть историю авто")
        return btn
    }()
    
    
    var delegate: MainVCCellActionsDelegate!
    
    // MARK: - Actions
    
    @objc private func openHistory () {
        delegate.openHistory()
    }
    
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CarHistoryCell: ConfigurableCell {
    
    func configure(data object: [MainViewModel.CarHistoryItems]?, index: Int) {
        if itemsStack.arrangedSubviews.count == 0 {
            for i in 0...1 {
                let item =  self.createItem(text: object![i].name, isVerified: object![i].data.is_already_purchased)
                if i == 1 {
                    let _ = Divider(parent: item, color: .lightBlueColor)
                }
                itemsStack.addArrangedSubview(item)
            }
        }
    }

    func returnData() -> [MainViewModel.CarHistoryItems]? {
        return nil
    }
}



extension CarHistoryCell {
    
    private func addViews () {
        self.contentView.addSubview(separatorView)
        self.contentView.addSubview(itemsStack)
        self.contentView.addSubview(titleLbl)
        self.contentView.addSubview(moreBtn)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(openHistory))
        moreBtn.addGestureRecognizer(tapGesture)
        
        titleLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(22)
            make.height.equalTo(20)
        }
        
        separatorView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
        }
        
        itemsStack.snp.makeConstraints { (make) in
            make.height.equalTo(98)
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(titleLbl.snp.bottom).offset(8)
        }
        
        moreBtn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-22)
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(itemsStack.snp.bottom).offset(8)
        }
    }
    
    private func createItem (text: String, isVerified: Bool = false) -> UIView {
        let view = UIView()
        
        let statusView = UIView()
        statusView.layer.cornerRadius = 8
        statusView.backgroundColor = isVerified ? .rgb(230, 245, 238) : .rgb(246, 228, 228)
        
        let iconView = UIImageView()
        iconView.image = UIImage(named: "verify")
        statusView.addSubview(iconView)
        
        let textLbl = UILabel()
        textLbl.numberOfLines = 0
        textLbl.sizeToFit()
        textLbl.font = .systemFont(ofSize: 15)
        let normText = isVerified ? "Найдены \(text)" : "Данные о \(text) не найдены"
        textLbl.text = normText
        
        view.addSubview(statusView)
        view.addSubview(textLbl)
        
        iconView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.center.equalToSuperview()
        }
        
        statusView.snp.makeConstraints { (make) in
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        textLbl.snp.makeConstraints { (make) in
            make.leading.equalTo(statusView.snp.trailing).offset(16)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        return view
    }
}


